package cn.exrick.xboot.modules.your.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.your.entity.FeedBack;
import cn.exrick.xboot.modules.your.service.IFeedBackService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyunxuan
 */
@Slf4j
@RestController
@Api(description = "陪护床管理接口")
@RequestMapping("/xboot/feedBack")
@Transactional
public class FeedBackController {

    @Autowired
    private IFeedBackService iFeedBackService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<FeedBack> get(@PathVariable String id){

        FeedBack feedBack = iFeedBackService.getById(id);
        return new ResultUtil<FeedBack>().setData(feedBack);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<FeedBack>> getAll(){

        List<FeedBack> list = iFeedBackService.list();
        return new ResultUtil<List<FeedBack>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<FeedBack>> getByPage(@ModelAttribute PageVo page, @ModelAttribute FeedBack feedBack){
        QueryWrapper<FeedBack> qw=new QueryWrapper<>();
        IPage<FeedBack> data = iFeedBackService.feedPage(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<FeedBack>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<FeedBack> saveOrUpdate(@ModelAttribute FeedBack feedBack){

        if(iFeedBackService.saveOrUpdate(feedBack)){
            return new ResultUtil<FeedBack>().setData(feedBack);
        }
        return new ResultUtil<FeedBack>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iFeedBackService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
